package com.game.ghost.webapp.apps.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.game.ghost.webapp.apps.data.PlayResponse;
import com.game.ghost.webapp.apps.service.GhostExecutor;
import com.game.ghost.webapp.apps.service.GhostWordsService;

/**
 * Controller class for allowing user to play game. 
 * 
 * @author gautak
 *
 */
@Controller
public class GhostGameController {

	private static Log logger_ = LogFactory.getLog(GhostGameController.class);

	@Autowired
	private GhostWordsService ghostWordsService;
	
	@Autowired
	private GhostExecutor ghostExecutor;

	
	@RequestMapping(value = "/play", method = RequestMethod.GET)
	public ModelAndView handleRequest() throws Exception {
		return new ModelAndView("ghost/play");
	}

	@RequestMapping(value = "/play/fetch", method = RequestMethod.GET)
	public @ResponseBody PlayResponse getPlayResponseInJSON(
			@RequestParam(value = "wordInPlay") String wordInPlay,
			@RequestParam(value = "enteredLetter") String enteredLetter) {

		PlayResponse playResponse = new PlayResponse();
		
		ghostExecutor.execute(wordInPlay, enteredLetter, playResponse);

		return playResponse;

	}
	
	@RequestMapping(value = "/play/clearcache", method = RequestMethod.GET)
	public ModelAndView clearCache() throws Exception {
		
		ghostWordsService.clearCache();
		
		return new ModelAndView("ghost/play");
	}
}
